package com.lanxiao.cloud.modules.file.domain.factory;

import com.lanxiao.cloud.common.core.error.LoginErrorType;
import com.lanxiao.cloud.common.core.exception.BusinessException;
import com.lanxiao.cloud.modules.file.domain.model.FileConfigure;
import com.lanxiao.cloud.modules.file.domain.service.IUploadFileService;
import com.lanxiao.cloud.modules.file.infrastructure.enums.UploadType;
import com.lanxiao.cloud.modules.file.infrastructure.error.UploadErrorType;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @author ：qiaoliang
 * @date ：2021-10-25
 */
@Component
public class UploadFileFactory implements ApplicationContextAware {

  private static Map<UploadType, IUploadFileService> UPLOAD_MAP;

  public IUploadFileService handle(UploadType type) {
    IUploadFileService uploadFileService = UPLOAD_MAP.get(type);
    BusinessException.isNull(uploadFileService, UploadErrorType.UPLOAD_TYPE_ERROR);
    return uploadFileService;
  }

  @Override
  public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    Map<String, IUploadFileService> beansOfType = applicationContext.getBeansOfType(IUploadFileService.class);
    UPLOAD_MAP = new HashMap<>(beansOfType.size());
    beansOfType.forEach((k,v)->{ UPLOAD_MAP.put(v.only(),v); });
  }


}
